﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using Ccit.OA.Utils;
using Ccit.Utils;
using System.Collections.Generic;
using Ccit.OA.MsSystemDoc;
using Newtonsoft.Json;
using System.Text;
namespace Ccit.OA.SystemDoc
{
    public partial class SystemDoc_class_edit : Ccit.Utils.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                this._hidClassType.Value = (null != Request.QueryString["classType"]) ? Request.QueryString["classType"].ToString() : "";
                this._hidSysDocId.Value = (null != Request.QueryString["SysDocId"]) ? Request.QueryString["SysDocId"].ToString() : "";
                this._hisState.Value = (null != Request.QueryString["State"]) ? Request.QueryString["State"].ToString() : "";
                ViewState["NodeId"] = (null != Request.QueryString["CreateNodeId"]) ? Request.QueryString["CreateNodeId"].ToString() : "";
                _hidCreate.Value = ViewState["NodeId"].ToString();
                ViewState["NodeVId"]="";
                InitPage();
                if (_hidSysDocId.Value != "")
                {  
                       SystemDocBLL Bll = new SystemDocBLL();
                       if (_hisState.Value == "2")
                       {
                           ViewState["NodeVId"] = Bll.GetNodeId(_hidSysDocId.Value);
                       }
                       else
                       {
                           if (_hidCreate.Value == "0")
                           {
                               ViewState["NodeVId"] = _hidSysDocId.Value;
                           }
                           else { ViewState["NodeVId"] = Bll.GetNodeId(_hidSysDocId.Value); }
                       }
                       Bll = null;
                       InitBinde(); 
                }
               
                bindDropDown(ddlUpAddr, ViewState["NodeVId"].ToString());
            }
        }
        private void InitPage()
        {
            SystemDocBLL Bll = new SystemDocBLL();
            string sWhere="", sFolderId, sClassType, sShare;
            sFolderId = _hidSysDocId.Value.Trim(); ;
            sClassType = _hidClassType.Value.Trim(); 
            sFolderId = "0"; 
            if (_hisState.Value == "2")
            {
                if (_hidSysDocId.Value != "")
                {
                    sWhere += "  and DOC_B2_10 <>" + _hidSysDocId.Value + " ";
                }
            } 
            
            sWhere += " and DOC_B2_40 =0 and DOC_B2_90 = '" + sClassType + "' and DOC_B2_100=0"; 
            List<MsSecondClass> msList = Bll.GetMsSecondItem(sWhere);
            if (msList.Count != 0)
            {
                ddlUpAddr.ClearSelection();
                ListItem List = new ListItem();
                List.Text = "无上级项目";
                List.Value = "0";
                ddlUpAddr.Items.Add(List);
                for (int i = 0; i < msList.Count; i++)
                {
                    sShare = Bll.GetShare(msList[i].Sysdocid);
                    List = new ListItem();
                    List.Text = HttpUtility.HtmlDecode("&nbsp;") + msList[i].Systypename;
                    List.Value = msList[i].Sysdocid;

                    ddlUpAddr.Items.Add(List);
                    DDBind(msList[i].Sysdocid, sClassType, "&nbsp;&nbsp;", sShare);
                }
            }
            Bll = null;
        }
        public void DDBind(string Superiorid, string classType, string sbEmpty, string sParaShare)
        {
            string sWhere="", sFolderId, sShare;
            sFolderId = _hidSysDocId.Value.Trim();
            sbEmpty += "&nbsp;&nbsp;";

            if (_hisState.Value == "2")
            {
                if (_hidSysDocId.Value != "")
                {
                    sWhere += "  and DOC_B2_10 <>" + _hidSysDocId.Value + " ";
                }
            } 
            sWhere += " and DOC_B2_40 IN(" + Superiorid + ") and DOC_B2_90 = '" + classType + "' and DOC_B2_100=0 ";
            SystemDocBLL Bll = new SystemDocBLL();
            List<MsSecondClass> msList = Bll.GetMsSecondItem(sWhere);

            if (msList.Count != 0)
            {

                for (int i = 0; i < msList.Count; i++)
                {
                    if (sParaShare == "01" || sParaShare == "00")
                    {
                        sShare = sParaShare;
                    }
                    else
                    {
                        sShare = Bll.GetShare(msList[i].Sysdocid);
                    }

                    ListItem List = new ListItem();
                    List.Text = HttpUtility.HtmlDecode(sbEmpty) + msList[i].Systypename;
                    List.Value = msList[i].Sysdocid ;

                    ddlUpAddr.Items.Add(List);
                    DDBind(msList[i].Sysdocid, classType, sbEmpty, sShare);
                }
            }
            Bll = null;
            msList = null;
        }
        private void InitBinde()
        {            
            SystemDocBLL Bll = new SystemDocBLL();
            _hidCreateId.Value = Bll.getAddrLayCodeById(_hidSysDocId.Value);
            MsSecondClass ms = Bll.GetSecondClassMess(_hidSysDocId.Value, _hidClassType.Value);
            Bll = null;
            if(ms!=null)
            {
                if (_hisState.Value == "2")
                {
                    txtName.Text = (ms.Systypename != "") ? ms.Systypename : "";
                    txtNumber.Text = ms.Sysnumber;
                    txtManage.Text = SysInterface.GetNameStrByIdStr(ms.Managemark, ms.Manageid, ",");
                    txtView.Text = SysInterface.GetNameStrByIdStr(ms.Viewmark, ms.Viewid, ",");
                    _hidManageid.Value = ms.Manageid;
                    _hidManageMark.Value = ms.Managemark;
                    _hidViewId.Value = ms.Viewid;
                    _hidViewMark.Value = ms.Viewmark;
                }
                else if (_hisState.Value == "1")
                {
                    _hidSysDocId.Value = "";
                    txtName.Text = (ms.Systypename != "") ? ms.Systypename : "";
                    txtNumber.Text = ms.Sysnumber;
                    txtManage.Text = SysInterface.GetNameStrByIdStr(ms.Managemark, ms.Manageid, ",");
                    txtView.Text = SysInterface.GetNameStrByIdStr(ms.Viewmark, ms.Viewid, ",");
                    _hidManageid.Value = ms.Manageid;
                    _hidManageMark.Value = ms.Managemark;
                    _hidViewId.Value = ms.Viewid;
                    _hidViewMark.Value = ms.Viewmark;
                }
                else
                {
                    _hidSysDocId.Value = "";
                }
            }
        }
        private void bindDropDown(DropDownList ddlUpAddr, string SysDocId)
        {
            
            if(ddlUpAddr.Items.Count==0)return ;
            int itemCount = ddlUpAddr.Items.Count; 
            for (int i = 0; i < itemCount; i++)
            {
               if (ddlUpAddr.Items[i].Value == SysDocId)
                {
                    ddlUpAddr.SelectedValue = SysDocId;
                    break;
                } 
            } 
        }
        /// <summary>
        /// save dep address info
        /// </summary>
        /// <param name="jsonExpr"></param>
        /// <param name="ctrlType"></param>
        /// <returns></returns>
        [global::System.Web.Services.WebMethod]
        public static string DoSave(string jsonExpr)
        {
            MsSecondClass ms = (MsSecondClass)JavaScriptConvert.DeserializeObject(jsonExpr, typeof(MsSecondClass));
            StringBuilder sb = new StringBuilder("<RESULT>");
            SystemDocBLL BLL = new SystemDocBLL();
            List<string> lstResult = BLL.DoSaveSysDocMess(ms);
            var newObj = new { resultSign = lstResult[0], message = lstResult[1], resultName = lstResult[2], hasChild = lstResult[3],isAddNew=lstResult[4], upAddrId = ms.Superiorid };
            string serExpr = JavaScriptConvert.SerializeObject(newObj); 
            sb.Append("<EXERESULT><![CDATA[").Append(serExpr).Append("]]></EXERESULT>");
            sb.Append("</RESULT>"); 
            ms = null;
            BLL = null;
            return sb.ToString();
        }

        protected void lnbCallBack_Click(object sender, EventArgs e)
        {
            SystemDocBLL Bll = new SystemDocBLL();
            _hidCreateId.Value = Bll.getAddrLayCodeById(_hidSysDocId.Value);
            MsSecondClass ms = Bll.GetSecondClassMess(_hidSysDocId.Value, _hidClassType.Value);
            txtManage.Text = SysInterface.GetNameStrByIdStr(ms.Managemark, ms.Manageid, ",");
            txtView.Text = SysInterface.GetNameStrByIdStr(ms.Viewmark, ms.Viewid, ",");
            _hidManageid.Value = ms.Manageid;
            _hidManageMark.Value = ms.Managemark;
            _hidViewId.Value = ms.Viewid;
            _hidViewMark.Value = ms.Viewmark;
        }
    }
}
